<?php


class testWakLogFile extends UnitTestCase {

    function testFactory() {
        // try getting a log for the day, no basename
        $path = $this->_getPath();
        $log = WakLogFile::getForDay($path);
        if ($this->assertIsA($log, 'WakLogFile'
            , "WakLogFile::getForDay(path) should return a WakLogFile")) {
            $this->assertEqual($log->basename(), date('Y-m-d').'.log'
                , "basename() should be {yyyy-mm-dd}.log");
        }

        // try getting a log for the day, with basename
        $path = $this->_getPath();
        $log = WakLogFile::getForDay($path, 'basename');
        if ($this->assertIsA($log, 'WakLogFile'
            , "WakLogFile::getForDay(path, basename) should return a WakLogFile")) {
            $this->assertEqual($log->basename(), 'basename.'.date('Y-m-d').'.log'
                , "basename() should be {basename}.{yyyy-mm-dd}.log");
        }

        // try getting a log for the day, with date
        $path = $this->_getPath();
        $log = WakLogFile::getForDay($path, null, '1976-01-10');
        if ($this->assertIsA($log, 'WakLogFile'
            , "WakLogFile::getForDay(path, null, '1976-01-10') should return a WakLogFile")) {
            $this->assertEqual($log->basename(), '1976-01-10.log'
                , "basename() should be 1976-01-10.log");
            $this->assertTrue($log->exists()
                , "exists() should return true when getting an existing log");
        }
    }

    function testWriting() {
        $path = $this->_getPath();
        $log = WakLogFile::getForDay($path, 'test_writing');
        // adding a first line
        $this->assertTrue($log->addEntry(array('11:22', 'something happened'))
            ,"addEntry(array) should return true" );
        $this->assertTrue($log->exists()
            ,"the log file should have been created");
        $size = $log->size();
        $this->assertTrue($size>0
            , "and should not be empty");
        // adding a second line
        $log->addEntry(array('11:55', 'another event'));
        $this->assertTrue($log->size()>$size
            ,"log should has grown after adding a second line");
        // checking content
        $content = '11:22'.chr(9).'something happened'.chr(13).chr(10)
        .'11:55'.chr(9).'another event'.chr(13).chr(10);
        $this->assertEqual($log->getContent(), $content
            , "content should match expected");
        $this->dump($content);

        // deleting file
        $log->delete();
    }

    function testReading() {
        
    }

    function _getPath() {
        $path = Webappkit::getPathFromId('webappkit.fs.log');
        return $path.'tests/fixtures/';
    }
}